package operations;

import MyThrowable.InputWrongful;
import student.Student;
import student.StudentList;

import java.util.Scanner;
import java.util.regex.Pattern;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 * Date: 2022-12-14
 * Time: 7:47
 */
public class FindOperation implements IOperation{
    static String PatternStr2 = "^[0-9]+$";
    static String PatternStr1 = "^[\\u4e00-\\u9fa5]{2,10}$";
    Scanner scan = new Scanner(System.in);
    @Override
    public void work(StudentList studentList) {
        System.out.println("查找学生!");
        if(studentList.isEmpty()){
            System.out.println("学生已经被清空!");
        }else {
            while (true) {
                int choice = menu();
                int flag = find(studentList, choice);
                if (choice == 0) {
                    break;
                }
            }
        }
    }
    public int menu() {
        while (true){
            System.out.println("*******************************");
            System.out.println("**********请选择查询方法:*********");
            System.out.println("********** 1.学号 **************");
            System.out.println("********** 2.姓名 **************");
            System.out.println("********** 0.返回上一级 *********");
            System.out.println("*******************************");
            String choice = scan.nextLine();
            if (Pattern.matches(PatternStr2, choice)) {
                return Integer.valueOf(choice);
            } else {
                System.out.println("输入有误!");
            }
        }
    }
   public int find(StudentList studentList,int choice) {
       switch (choice) {
           case 1:
               System.out.println("请输入学号:");
               while (true) {
                   String id = "00";
                   while (true) {
                       id = scan.nextLine();
                       try {
                           if (!Pattern.matches(PatternStr2, id)) {
                               throw new InputWrongful("输入有误,请再次输入!");
                           } else {
                               break;
                           }
                       } catch (InputWrongful e) {
                           e.printStackTrace();
                       }
                   }
                   int pos = studentList.findIDpos(Integer.valueOf(id));
                   if (pos == -1) {
                       System.out.println("该学号不存在");
                       break;
                   } else {
                       System.out.println(studentList.students.get(pos));
                       break;
                   }
               }
               break;
           case 2:
               System.out.println("请输入姓名:");
               String name = "00";
               while (true) {
                   while (true) {
                       name = scan.nextLine();
                       try {
                           if (!Pattern.matches(PatternStr1, name)) {
                               throw new InputWrongful("输入有误,请再次输入!");
                           } else {
                               break;
                           }
                       } catch (InputWrongful e) {
                           e.printStackTrace();
                       }
                   }
                   int pos = studentList.findNamepos(name);
                   if (pos == -1) {
                       System.out.println("该学生不存在");
                       System.out.println("请重新输入!");
                   } else {
                       System.out.println(studentList.students.get(pos));
                       break;
                   }
               }
               break;
           case 0:
               break;
           default:
               System.out.println("请重新输入!");
               return 1;
       }
       return 0;
   }
}
